//
//  MainController.swift
//  foreign_swift
//
//  Created by hdr on 15-4-19.
//  Copyright (c) 2015年 hdr. All rights reserved.
//

import UIKit

let contentFrame = CGRect(x: 0, y: 0, width: Int(Const.screen_width), height: Int(Const.screen_height) - dock_height)

class MainController:UIViewController ,DockClickDelegate,UINavigationControllerDelegate{
   
    
    var currentController:UIViewController?
    
    var dock:Dock!
    
    init() {
        super.init(nibName: nil, bundle: nil)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        
        //初始化导航栏的样式
        initNavigationBar()
        
        //创建子controller
        createChildViewController()
        
        //添加底部的四个dock
        addDock()

        
    }
    
    override func addChildViewController(childController: UIViewController) {
        let nav = UINavigationController(rootViewController: childController)
        nav.delegate = self
        super.addChildViewController(nav)
    }
    
    func onDockClick(dockIndex: Int) {
        if let currentController = self.currentController {
            currentController.view.removeFromSuperview()
        }
        
        currentController = self.childViewControllers[dockIndex] as? UIViewController
        if let currentController = self.currentController {
            currentController.view.frame = contentFrame
            self.view.addSubview(currentController.view)
        }
        
    }
    
    func createChildViewController(){
        addChildViewController(MeasureController())
        
        addChildViewController(ServiceController())
        
        addChildViewController(SystemController())
        
        addChildViewController(UserController())
    }
    
    func initNavigationBar(){
        //设置导航栏的默认样式
        UIApplication.sharedApplication().statusBarHidden = false
        
        UINavigationBar.appearance().barTintColor = UIColor.themeColor
        
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor(),NSFontAttributeName:UIFont.systemFontOfSize(22)]
        
        UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
        
    }
    
    func addDock(){
        dock = Dock(frame: CGRect(x: 0, y: Int(Const.screen_height) - dock_height, width:Int(Const.screen_width), height: dock_height))
        dock.dockClickDelegate = self
        dock.adjustView()
        
//        dock.setSelectBtnView(3)
        
        self.view.addSubview(dock)
    }
}
